package com.xzuf.datatransfer;

import java.io.Serializable;
import java.util.Vector;

public class TableVO implements Serializable {

	public String m_strTableName;
	private ColumnVO m_Columns[];
	public Boolean m_isSelected;

	public TableVO() {
		m_strTableName = null;
		m_Columns = null;
		m_isSelected = new Boolean(false);
	}

	public Object clone() {
		Object o = null;
		try {
			o = super.clone();
		} catch (Exception exception) {
		}
		TableVO vo = (TableVO) o;
		ColumnVO cols[] = getColumns();
		if (cols != null) {
			int n = cols.length;
			ColumnVO newCols[] = new ColumnVO[n];
			for (int i = 0; i < n; i++)
				newCols[i] = (ColumnVO) cols[i].clone();

			vo.setColumns(newCols);
		}
		vo.setSelected(isSelected());
		vo.setTableName(getTableName());
		return vo;
	}

	public ColumnVO[] getColumns() {
		return m_Columns;
	}

	public ColumnVO getColVOByColName(String colName) {
		if (colName == null)
			return null;
		int index = colName.indexOf(46);
		if (index != -1)
			colName = colName.substring(index + 1);
		ColumnVO vo = null;
		ColumnVO allCols[] = getColumns();
		int i = 0;
		for (int n = allCols != null ? allCols.length : 0; i < n; i++) {
			if (!allCols[i].getColumnName().equalsIgnoreCase(colName.trim()))
				continue;
			vo = allCols[i];
			break;
		}

		return vo;
	}

	public SelSqlVO getDefaultSelSqlVO() {
		ColumnVO cols[] = getColumns();
		int count = cols != null ? cols.length : 0;
		StringBuffer sbSelectsub = new StringBuffer();
		for (int i = 0; i < count; i++) {
			if (i != 0)
				sbSelectsub.append(", ");
			sbSelectsub.append(getTableName()).append(".").append(
					cols[i].getColumnName());
		}

		SelSqlVO sqlVO = new SelSqlVO();
		sqlVO.setSelectSub(sbSelectsub.toString());
		sqlVO.setFromSub(getTableName());
		return sqlVO;
	}

	public String getEntityName() {
		return null;
	}

	public ColumnVO[] getPKColumns() {
		ColumnVO cols[] = getColumns();

		int colCount = cols != null ? cols.length : 0;
		Vector v = new Vector();
		for (int i = 0; i < colCount; i++) {
			if (cols[i].getColumnName().equalsIgnoreCase("PK_RELEASE")) {
				cols[i].m_bIsPK = true;
				cols[i].setIsPK(true);
			}
			if (cols[i].isPK())
				v.add(cols[i]);
		}
		ColumnVO pkCols[] = new ColumnVO[v.size()];
		if (v.size() > 0)
			v.copyInto(pkCols);
		return pkCols;
	}

	public String getTableName() {
		return m_strTableName;
	}

	public Boolean isSelected() {
		return m_isSelected;
	}

	public void setColumns(ColumnVO newColumns[]) {
		m_Columns = newColumns;
	}

	public void setSelected(Boolean isSelected) {
		m_isSelected = isSelected;
	}

	public void setTableName(String newTableName) {
		m_strTableName = newTableName;
	}

	public String toString() {
		return m_strTableName;
	}

}
